using System;
using UnityEngine;

public class AI_plane : AIController
{
	public AIPLANESTATE CurAIState;

	public Vector3 curTargetPos;

	private float maxspeed;

	private float accspeed = 20f;

	private float normalspeed;

	private float machineshotspeed = 900f;

	public Transform leftfire;

	public Transform rightfire;

	public int bullet_hurtdamage;

	public int bullet_penetrationVal;

	public int missile_hurtdamage;

	public int missile_penetrationVal;

	public Transform youdaodan_point;

	public Vector3 tempdir;

	public float maxangleZ = 45f;

	public float curangleZ;

	public Vector3 patrolpoint;

	public float patroldis;

	private Vector3 tempcalpos;

	private Vector3 tempcaldir;

	private int calangle;

	private bulletmove curbullet;

	private missilemove curmissile;

	private bool isexplo;

	private bool ishaveyoudaodan;

	private float timeofyoudao;

	public bool havemiss;

	private Effectobj tempeffect;

	public Vector3 playeraim;

	private bool isleftshot;

	private bool haveshotmissile;

	private int bulletnum;

	private int bullet_limit = 10;

	private bool isavoid;

	private bool repatrol;

	private Vector3 inversepos;

	private bool isguide;

	private Vector3 dirforward;

	public float rotatediss;

	private float curspeed;

	public bool istakeoff;

	public float up;

	private float scale_ratio;

	private float dis;

	public bool ishavescale;

	private void Start()
	{
		init_plane();
	}

	public void init_plane()
	{
		if (!isinit)
		{
			m_transform = base.transform;
			patrolpoint = m_transform.position;
			playertra = plane_smothfollow.instance.plane_body;
			playerbulletspeed = plane_smothfollow.instance.machinespeed;
			curTargetPos = patrolpoint;
			maincam = plane_smothfollow.instance.mainCam;
			bodyself.localRotation = Quaternion.Euler(0f, 0f, 0f);
			isexplo = false;
			effectpoint = bodyself;
			lowhpsmoke = null;
			explosionobj = null;
			isinit = true;
		}
	}

	public void init_info(Attribute curatt)
	{
		isdie = false;
		CurAIState = AIPLANESTATE.flyto;
		Cur_att = curatt;
		AIspeed = curatt.maxspeed;
		normalspeed = curatt.maxspeed;
		maxspeed = UnityEngine.Random.Range(160, 180);
		if (GlobalInf.gamefirst)
		{
			bullet_hurtdamage = 2;
			bullet_penetrationVal = 10;
			missile_hurtdamage = 4;
			missile_penetrationVal = 10;
			havemiss = false;
			health_con.set_heal(curatt.curhealatt);
			CurAIState = AIPLANESTATE.guide;
		}
		else
		{
			bullet_hurtdamage = curatt.curweapon.machgun;
			bullet_penetrationVal = curatt.curweapon.machgun_pen;
			missile_hurtdamage = curatt.curweapon.missile;
			missile_penetrationVal = curatt.curweapon.missile_pen;
			havemiss = curatt.curweapon.havemiss;
			health_con.set_heal(curatt.curhealatt);
		}
		if (GlobalInf.CurrentQuality > Quality.Low)
		{
			ishavescale = true;
			return;
		}
		ishavescale = false;
		bodyself.transform.localScale = new Vector3(1f, 1f, 1f);
	}

	public override void Reset_AI()
	{
		base.Reset_AI();
		init_plane();
		init_info(Cur_att);
		changeHP();
	}

	private void Update()
	{
		if (ishavescale)
		{
			scaleupdate();
		}
		if (isdie)
		{
			diedfun();
			diedmove();
			return;
		}
		if (istakeoff)
		{
			takeoff_update();
			return;
		}
		if (targetTrans == null)
		{
			findTarget();
			targettime = 0f;
			return;
		}
		targettime += Time.deltaTime;
		if (targettime > 30f)
		{
			findTarget();
			targettime = 0f;
			return;
		}
		switch (CurAIState)
		{
			case AIPLANESTATE.patrol:
				patrolstate();
				break;
			case AIPLANESTATE.flyto:
				flytostate();
				break;
			case AIPLANESTATE.fight:
				fightstate();
				break;
			case AIPLANESTATE.avoid:
				avoidstate();
				break;
			case AIPLANESTATE.guide:
				guidestate();
				break;
		}
		movetotarget();
		youdaodanFire();
	}

	public void diedmove()
	{
		m_transform.Translate(0f, 0f, AIspeed * Time.deltaTime);
		m_transform.rotation = Quaternion.Lerp(m_transform.rotation, Quaternion.Euler(85f, 0f, 0f), Time.deltaTime * 0.2f);
		bodyself.Rotate(0f, 0f, Time.deltaTime * 70f, Space.Self);
		if (diedtime > 4f && !isexplo)
		{
			isexplo = true;
			if (explosionobj != null)
			{
				UnityVersionUtil.SetActiveRecursive(explosionobj.gameObject, state: false);
				UnityVersionUtil.SetActiveRecursive(explosionobj.gameObject, state: true);
			}
		}
	}

	public void youdaodanFire()
	{
		if (!havemiss)
		{
			return;
		}
		if (ishaveyoudaodan)
		{
			timeofyoudao += Time.deltaTime;
			if (timeofyoudao > 60f)
			{
				ishaveyoudaodan = false;
				timeofyoudao = 0f;
			}
		}
		if (!ismissileaim || ishaveyoudaodan)
		{
			return;
		}
		if (curaimmissilelist.Count > 0)
		{
			for (int i = 0; i < curaimmissilelist.Count; i++)
			{
				if (Vector3.Distance(curaimmissilelist[i].m_transform.position, m_transform.position) < 100f)
				{
					if (UnityEngine.Random.Range(0, 10) > 7)
					{
						youdaodanfire();
						timeofyoudao = 0f;
						ishaveyoudaodan = true;
					}
					ismissileaim = false;
				}
			}
		}
		else
		{
			ismissileaim = false;
		}
	}

	public void youdaodanfire()
	{
		tempeffect = EffectPool.instance.GetEffectObj(EFFECTTYPE.youdaodan);
		if (tempeffect != null)
		{
			tempeffect.transform.parent = youdaodan_point;
			tempeffect.transform.localPosition = Vector3.zero;
			tempeffect.transform.localRotation = Quaternion.identity;
			tempeffect.showeffect();
		}
		for (int i = 0; i < curaimmissilelist.Count; i++)
		{
			curaimmissilelist[i].targetAI = null;
			curaimmissilelist[i].ishaveaim = false;
		}
		curaimmissilelist.Clear();
	}

	public void patrolstate()
	{
		if (Vector3.Distance(m_transform.position, curTargetPos) <= AIspeed)
		{
			curTargetPos = getRandomPoint();
			if (curTargetPos.y < 450f || curTargetPos.y > 2000f)
			{
				curTargetPos.y = UnityEngine.Random.Range(4, 11) * 200;
			}
			if (Mathf.Abs(curTargetPos.x - GlobalInf.centerpoint.x) > (float)GlobalInf.flyrange_AI)
			{
				curTargetPos.x = (float)(UnityEngine.Random.Range(-6, 7) * 200) + GlobalInf.centerpoint.x;
			}
			if (Mathf.Abs(curTargetPos.z - GlobalInf.centerpoint.z) > (float)GlobalInf.flyrange_AI)
			{
				curTargetPos.z = (float)(UnityEngine.Random.Range(-6, 7) * 200) + GlobalInf.centerpoint.z;
			}
		}
		if (Vector3.Distance(m_transform.position, targetTrans.position) < FightDistance)
		{
			CurAIState = AIPLANESTATE.fight;
		}
	}

	public Vector3 getRandomPoint()
	{
		tempcaldir = m_transform.position - patrolpoint;
		tempcaldir.y = 0f;
		calangle = (calangle + UnityEngine.Random.Range(45, 100)) % 360;
		tempcaldir = new Vector3(Mathf.Cos((float)calangle * ((float)Math.PI / 180f)), 0f, Mathf.Sin((float)calangle * ((float)Math.PI / 180f)));
		tempcalpos = patrolpoint + -tempcaldir * patroldis + Vector3.up * UnityEngine.Random.Range(-30, 30);
		return tempcalpos;
	}

	public void flytostate()
	{
		curTargetPos = targetTrans.position;
		if (Vector3.Distance(m_transform.position, targetTrans.position) < FightDistance)
		{
			CurAIState = AIPLANESTATE.fight;
		}
		if (!(Mathf.Abs(curTargetPos.x - GlobalInf.centerpoint.x) > (float)GlobalInf.flyrange_AI) && !(Mathf.Abs(curTargetPos.z - GlobalInf.centerpoint.z) > (float)GlobalInf.flyrange_AI))
		{
			Vector3 position = m_transform.position;
			if (!(position.y < 450f))
			{
				return;
			}
		}
		CurAIState = AIPLANESTATE.avoid;
		isavoid = false;
		repatrol = true;
	}

	public void fightstate()
	{
		if (AIspeed > normalspeed)
		{
			AIspeed -= Time.deltaTime * accspeed;
		}
		curTargetPos = targetTrans.position;
		if (Vector3.Angle(m_transform.forward, targetTrans.position - m_transform.position) < 10f)
		{
			if (GlobalInf.isInternalInjury)
			{
				if (targetAI == null)
				{
					playeraim = plane_smothfollow.instance.playerAim(m_transform.position, machineshotspeed);
				}
				else
				{
					playeraim = targetAI.CalcAimPos(m_transform.position, machineshotspeed);
				}
			}
			else if (targetFri == null)
			{
				playeraim = plane_smothfollow.instance.playerAim(m_transform.position, machineshotspeed);
			}
			else
			{
				playeraim = targetFri.CalcAimPos(m_transform.position, machineshotspeed);
			}
			if (bulletnum <= bullet_limit)
			{
				if (isleftshot)
				{
					curbullet = NPCbulletpool.instance.GetBullet();
					if (curbullet != null)
					{
						curbullet.transform.position = leftfire.position;
						curbullet.transform.rotation = leftfire.rotation;
						curbullet.targetAI = null;
						curbullet.isplayerbullet = false;
						curbullet.movespeed = machineshotspeed;
						curbullet.endpos = playeraim;
						curbullet.shot(bullet_hurtdamage, bullet_penetrationVal, TypeIndex);
						bulletnum++;
						isleftshot = false;
					}
				}
				else
				{
					curbullet = NPCbulletpool.instance.GetBullet();
					if (curbullet != null)
					{
						curbullet.transform.position = rightfire.position;
						curbullet.transform.rotation = rightfire.rotation;
						curbullet.targetAI = null;
						curbullet.isplayerbullet = false;
						curbullet.movespeed = machineshotspeed;
						curbullet.endpos = playeraim;
						curbullet.shot(bullet_hurtdamage, bullet_penetrationVal, TypeIndex);
						bulletnum++;
						isleftshot = true;
					}
				}
			}
			if (!haveshotmissile)
			{
				Vector3 vector = targetTrans.InverseTransformPoint(m_transform.position);
				if (vector.z < -100f)
				{
					curmissile = NPCmissilepool.instance.GetMissile();
					if (curmissile != null)
					{
						curmissile.transform.position = leftfire.position;
						curmissile.transform.rotation = leftfire.rotation;
						if (GlobalInf.isInternalInjury)
						{
							curmissile.targetai_fri = null;
							curmissile.targetAI = targetAI;
						}
						else
						{
							curmissile.targetAI = null;
							curmissile.targetai_fri = targetFri;
						}
						curmissile.targetTra = targetTrans;
						curmissile.isplayerbullet = false;
						curmissile.shot(missile_hurtdamage, missile_penetrationVal, TypeIndex, AIspeed);
						haveshotmissile = true;
					}
				}
			}
		}
		else
		{
			haveshotmissile = false;
			bulletnum = 0;
		}
		if (!(Vector3.Distance(m_transform.position, targetTrans.position) < FightDistance * 0.2f))
		{
			Vector3 position = m_transform.position;
			if (!(position.y < 450f))
			{
				goto IL_0465;
			}
		}
		CurAIState = AIPLANESTATE.avoid;
		isavoid = false;
		repatrol = true;
		goto IL_0465;
	IL_0465:
		if (!(Mathf.Abs(curTargetPos.x - GlobalInf.centerpoint.x) > (float)GlobalInf.flyrange_AI) && !(Mathf.Abs(curTargetPos.z - GlobalInf.centerpoint.z) > (float)GlobalInf.flyrange_AI))
		{
			Vector3 position2 = m_transform.position;
			if (!(position2.y < 450f))
			{
				return;
			}
		}
		CurAIState = AIPLANESTATE.avoid;
		isavoid = false;
		repatrol = true;
	}

	public void avoidstate()
	{
		if (AIspeed < maxspeed)
		{
			AIspeed += Time.deltaTime * accspeed;
		}
		if (!isavoid)
		{
			inversepos = targetTrans.InverseTransformPoint(m_transform.position);
			curTargetPos = targetTrans.position + Mathf.Sign(inversepos.x) * targetTrans.right * FightDistance * 1f + Mathf.Sign(inversepos.y) * Vector3.up * FightDistance * 0.2f - Mathf.Sign(inversepos.z) * targetTrans.forward * FightDistance * 0.2f;
			if (curTargetPos.y < 450f || curTargetPos.y > 2000f)
			{
				curTargetPos.y = UnityEngine.Random.Range(4, 11) * 200;
			}
			if (Mathf.Abs(curTargetPos.x - GlobalInf.centerpoint.x) > (float)GlobalInf.flyrange_AI)
			{
				curTargetPos.x = (float)(UnityEngine.Random.Range(-6, 7) * 200) + GlobalInf.centerpoint.x;
			}
			if (Mathf.Abs(curTargetPos.z - GlobalInf.centerpoint.z) > (float)GlobalInf.flyrange_AI)
			{
				curTargetPos.z = (float)(UnityEngine.Random.Range(-6, 7) * 200) + GlobalInf.centerpoint.z;
			}
			isavoid = true;
			repatrol = false;
		}
		if (Vector3.Distance(m_transform.position, curTargetPos) < AIspeed && !repatrol)
		{
			CurAIState = AIPLANESTATE.fight;
			repatrol = true;
		}
		if (Vector3.Distance(m_transform.position, targetTrans.position) > FightDistance * 1.2f)
		{
			CurAIState = AIPLANESTATE.fight;
		}
	}

	public void guidestate()
	{
		targetTrans = plane_smothfollow.instance.plane_body;
		inversepos = targetTrans.InverseTransformPoint(m_transform.position);
		if (Vector3.Angle(inversepos, Vector3.forward) > 10f)
		{
			AIspeed = 200f;
		}
		else if (Vector3.Distance(m_transform.position, targetTrans.position) < 100f)
		{
			AIspeed = PlaneMoveController.instance.speed + 10f;
		}
		else
		{
			AIspeed = PlaneMoveController.instance.speed - 10f;
		}
		if (!isguide)
		{
			dirforward = targetTrans.forward;
			dirforward.y = 0f;
			dirforward = dirforward.normalized;
			curTargetPos = targetTrans.position - targetTrans.right * FightDistance * 0.01f + dirforward * FightDistance * 0.2f;
			isguide = true;
			if (curTargetPos.y < 450f || curTargetPos.y > 2000f)
			{
				curTargetPos.y = UnityEngine.Random.Range(4, 11) * 200;
			}
			if (Mathf.Abs(curTargetPos.x - GlobalInf.centerpoint.x) > (float)GlobalInf.flyrange_AI)
			{
				curTargetPos.x = (float)(UnityEngine.Random.Range(-6, 7) * 200) + GlobalInf.centerpoint.x;
			}
			if (Mathf.Abs(curTargetPos.z - GlobalInf.centerpoint.z) > (float)GlobalInf.flyrange_AI)
			{
				curTargetPos.z = (float)(UnityEngine.Random.Range(-6, 7) * 200) + GlobalInf.centerpoint.z;
			}
		}
		else
		{
			if (Vector3.Distance(m_transform.position, curTargetPos) < 100f)
			{
				isguide = false;
			}
			inversepos = targetTrans.InverseTransformPoint(curTargetPos);
			if (Vector3.Angle(inversepos, Vector3.forward) > 10f)
			{
				isguide = false;
			}
		}
	}

	public void movetotarget()
	{
		m_transform.Translate(0f, 0f, AIspeed * Time.deltaTime);
		tempdir = m_transform.InverseTransformPoint(curTargetPos);
		m_transform.rotation = Quaternion.Slerp(m_transform.rotation, Quaternion.LookRotation(curTargetPos - m_transform.position), Time.deltaTime * rotatediss);
		Vector3 localEulerAngles = bodyself.localEulerAngles;
		curangleZ = localEulerAngles.z;
		if (Mathf.Abs(tempdir.x) > 50f)
		{
			curangleZ = Mathf.LerpAngle(curangleZ, (0f - tempdir.x) / Mathf.Abs(tempdir.x) * maxangleZ, Time.deltaTime * 2f);
		}
		else
		{
			curangleZ = Mathf.LerpAngle(curangleZ, (0f - tempdir.x) / 50f * maxangleZ, Time.deltaTime * 2f);
		}
		bodyself.localRotation = Quaternion.Euler(0f, 0f, curangleZ);
	}

	public void take_off(Vector3 Bpos, Vector3 Epos)
	{
		Reset_AI();
		curspeed = 0f;
		up = 0f;
		m_transform.position = Bpos;
		m_transform.rotation = Quaternion.Euler(Vector3.zero);
		m_transform.forward = (Epos - Bpos).normalized;
		UnityVersionUtil.SetActiveRecursive(m_transform.gameObject, state: true);
		istakeoff = true;
	}

	public void takeoff_update()
	{
		if (curspeed < AIspeed)
		{
			curspeed += Time.deltaTime * 10f;
		}
		else
		{
			istakeoff = false;
		}
		if (curspeed > 0.5f * AIspeed)
		{
			up = (curspeed - 0.5f * AIspeed) * 0.2f;
		}
		m_transform.Translate(0f, up * Time.deltaTime, curspeed * Time.deltaTime);
	}

	public void scaleupdate()
	{
		dis = Vector3.Distance(maincam.transform.position, m_transform.position);
		if (dis > FightDistance * 0.5f)
		{
			dis = FightDistance * 0.5f;
		}
		scale_ratio = 0.002f * dis + 0.8f;
		if (scale_ratio < 1f)
		{
			scale_ratio = 1f;
		}
		bodyself.transform.localScale = new Vector3(scale_ratio, scale_ratio, scale_ratio);
	}
}
